package com.ruoyi.dzzh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.dzzh.domain.FilePts;
import com.ruoyi.dzzh.mapper.DzzhPtsMapper;
import com.ruoyi.dzzh.domain.DzzhPts;
import com.ruoyi.dzzh.service.IDzzhPtsService;

/**
 * 灾害点Service业务层处理
 * 
 * @author cjt
 * @date 2023-04-07
 */
@Service
public class DzzhPtsServiceImpl implements IDzzhPtsService 
{
    @Autowired
    private DzzhPtsMapper dzzhPtsMapper;

    /**
     * 查询灾害点
     * 
     * @param ptsId 灾害点主键
     * @return 灾害点
     */
    @Override
    public DzzhPts selectDzzhPtsByPtsId(Long ptsId)
    {
        return dzzhPtsMapper.selectDzzhPtsByPtsId(ptsId);
    }

    /**
     * 查询灾害点列表
     * 
     * @param dzzhPts 灾害点
     * @return 灾害点
     */
    @Override
    public List<DzzhPts> selectDzzhPtsList(DzzhPts dzzhPts)
    {
        return dzzhPtsMapper.selectDzzhPtsList(dzzhPts);
    }

    /**
     * 新增灾害点
     * 
     * @param dzzhPts 灾害点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDzzhPts(DzzhPts dzzhPts)
    {
        dzzhPts.setCreateTime(DateUtils.getNowDate());
        int rows = dzzhPtsMapper.insertDzzhPts(dzzhPts);
        insertFilePts(dzzhPts);
        return rows;
    }

    /**
     * 修改灾害点
     * 
     * @param dzzhPts 灾害点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDzzhPts(DzzhPts dzzhPts)
    {
        dzzhPts.setUpdateTime(DateUtils.getNowDate());
        dzzhPtsMapper.deleteFilePtsByPtsId(dzzhPts.getPtsId());
        insertFilePts(dzzhPts);
        return dzzhPtsMapper.updateDzzhPts(dzzhPts);
    }

    /**
     * 批量删除灾害点
     * 
     * @param ptsIds 需要删除的灾害点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzzhPtsByPtsIds(Long[] ptsIds)
    {
        dzzhPtsMapper.deleteFilePtsByPtsIds(ptsIds);
        return dzzhPtsMapper.deleteDzzhPtsByPtsIds(ptsIds);
    }

    /**
     * 删除灾害点信息
     * 
     * @param ptsId 灾害点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzzhPtsByPtsId(Long ptsId)
    {
        dzzhPtsMapper.deleteFilePtsByPtsId(ptsId);
        return dzzhPtsMapper.deleteDzzhPtsByPtsId(ptsId);
    }

    /**
     * 新增现场照片信息
     * 
     * @param dzzhPts 灾害点对象
     */
    public void insertFilePts(DzzhPts dzzhPts)
    {
        List<FilePts> filePtsList = dzzhPts.getFilePtsList();
        Long ptsId = dzzhPts.getPtsId();
        if (StringUtils.isNotNull(filePtsList))
        {
            List<FilePts> list = new ArrayList<FilePts>();
            for (FilePts filePts : filePtsList)
            {
                filePts.setPtsId(ptsId);
                list.add(filePts);
            }
            if (list.size() > 0)
            {
                dzzhPtsMapper.batchFilePts(list);
            }
        }
    }
}
